{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-30T16:27:19.650241Z",
     "start_time": "2021-11-30T16:27:19.642241Z"
    }
   },
   "outputs": [],
   "source": [
    "class Text_statistics(object):\n",
    "    '''\n",
    "    中文文本统计的类\n",
    "    '''\n",
    "    def __init__(self, name): # 实例化的传入的名字\n",
    "        self.name = name\n",
    "        \n",
    "    \n",
    "    def pri(self):\n",
    "        import re\n",
    "        shuru = input(\"请输入文件完整路径:\")\n",
    "        self.lujing=shuru\n",
    "        line_count=0\n",
    "        character_count=0\n",
    "        zimu_con=0        \n",
    "        shuzi_con=0\n",
    "        hanzi_con=0\n",
    "        kongge_con=0\n",
    "        with open(shuru,'r',encoding= 'utf-8') as f:\n",
    "            for line in f:\n",
    "                line_count+=1\n",
    "                character_count+=len(line)\n",
    "                sz_count=0\n",
    "                zm_count=0\n",
    "                hz_count=0\n",
    "                kg_count=0                \n",
    "                for i in line:\n",
    "                    zhmodel = re.compile(u'[\\u4e00-\\u9fa5]')\n",
    "                    if i.isdigit():\n",
    "                        sz_count+=1\n",
    "                    elif zhmodel.search(i):\n",
    "                        hz_count+=1\n",
    "                    elif i.isalpha():\n",
    "                        zm_count+=1\n",
    "                    elif i.isspace():\n",
    "                        kg_count+=1\n",
    "                shuzi_con+=sz_count\n",
    "                zimu_con+=zm_count\n",
    "                hanzi_con+=hz_count\n",
    "                kongge_con+=kg_count\n",
    "        print('段落数：',line_count)\n",
    "        print('总字数：',character_count)\n",
    "        print('包含数字字数：',shuzi_con)\n",
    "        print('包含字母字数：',zimu_con)\n",
    "        print('包含汉字字数：',hanzi_con)\n",
    "        print('包含空格字数：',kongge_con)\n",
    "    \n",
    "    def ser(self):\n",
    "        shuru2 = input(\"请输入你要查询的汉字:\")\n",
    "        shuru=self.lujing\n",
    "        hanzi_ser=0\n",
    "        with open(shuru,'r',encoding= 'utf-8') as f:\n",
    "            for line in f:\n",
    "                hz_ser=0\n",
    "                for i in line:\n",
    "                    if i==shuru2:\n",
    "                        hz_ser+=1\n",
    "                hanzi_ser+=hz_ser\n",
    "        print('查询到的字数有：',hanzi_ser)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-30T16:27:24.068641Z",
     "start_time": "2021-11-30T16:27:24.063641Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'文本分析'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=Text_statistics('文本分析')\n",
    "a.name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-30T16:26:38.689441Z",
     "start_time": "2021-11-30T16:26:34.454441Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入文件完整路径:./中文文本_测试用.txt\n",
      "段落数： 6\n",
      "总字数： 684\n",
      "包含数字字数： 0\n",
      "包含字母字数： 0\n",
      "包含汉字字数： 610\n",
      "包含空格字数： 6\n"
     ]
    }
   ],
   "source": [
    "a.pri()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-30T16:27:28.591441Z",
     "start_time": "2021-11-30T16:27:26.423441Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要查询的汉字:野\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'Text_statistics' object has no attribute 'lujing'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-13ca3bae5777>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-12-1340925a6c1c>\u001b[0m in \u001b[0;36mser\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     48\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     49\u001b[0m         \u001b[0mshuru2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"请输入你要查询的汉字:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 50\u001b[1;33m         \u001b[0mshuru\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlujing\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     51\u001b[0m         \u001b[0mhanzi_ser\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     52\u001b[0m         \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mshuru\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Text_statistics' object has no attribute 'lujing'"
     ]
    }
   ],
   "source": [
    "a.ser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
